package com.dispatch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.sys.location.entity.Everyday;
import com.sys.location.service.EverydayService;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Component
@Slf4j
public class TestDispatch {
	
	@Autowired
	EverydayService everydayService;
	
	
	
	/**
	 * 每日一句
	 * 每天凌晨0点调度一次
	 */
	@Scheduled(cron="0 0 0 * * ?")
	public void everyday() {
		  String url ="http://open.iciba.com/dsapi/";//金山词霸每日一句
	      BufferedReader in=null;
	      try {
		      URL weChatUrl = new URL(url);
		      URLConnection connection = weChatUrl.openConnection();
		      connection.setConnectTimeout(5000);
		      connection.setReadTimeout(5000);
		      connection.connect();
		      in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		      JSONObject jsonObject = JSONObject.fromObject(in.readLine());
		      String sid=jsonObject.getString("sid");
		      String tts=jsonObject.getString("tts");
		      String content=jsonObject.getString("content");
		      String note=jsonObject.getString("note");
		      String love=jsonObject.getString("love");
		      String translation=jsonObject.getString("translation");
		      String picture=jsonObject.getString("picture");
		      String picture2=jsonObject.getString("picture2");
		      String caption=jsonObject.getString("caption");
		      String dateline=jsonObject.getString("dateline");
		      String sPv=jsonObject.getString("s_pv");
		      String spPv=jsonObject.getString("sp_pv");
		      String fenxiangImg=jsonObject.getString("fenxiang_img");
		      String extendInfo=jsonObject.toString();
		      Everyday everyday=new Everyday();
		      everyday.setCaption(caption);
		      everyday.setContent(content);
		      everyday.setDateline(dateline);
		      everyday.setExtendInfo(extendInfo);
		      everyday.setFenxiangImg(fenxiangImg);
		      everyday.setLove(love);
		      everyday.setNote(note);
		      everyday.setPicture(picture);
		      everyday.setPicture2(picture2);
		      everyday.setSid(sid);
		      everyday.setSpPv(spPv);
		      everyday.setSPv(sPv);
		      everyday.setTts(tts);
		      everyday.setTranslation(translation);
		      everyday.setCreateTime(new Date());
		      everydayService.insert(everyday);
		      log.info("每日一句定时任务已完毕");
	      }catch (Exception e) {
	    	  log.info("错误:"+e);
	      }finally {
	    	  try {
				if(in!=null) {
					in.close();
				}
			} catch (Exception e2) {
				 log.info("发送错误:"+e2);
			}
	      }
		
	}
	
	
}
